package com.woniuxy.bookmall.controller;

import com.woniuxy.bookmall.entity.TAddress;
import com.woniuxy.bookmall.entity.TItem;
import com.woniuxy.bookmall.entity.TUser;
import com.woniuxy.bookmall.service.AddressService;
import com.woniuxy.bookmall.service.CartService;
import com.woniuxy.bookmall.vo.Cart;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

@Controller
@Slf4j
public class CartController {

    @Autowired
    CartService cartService ;

    @Autowired
    AddressService addressService ;

    @RequestMapping("/cart/calCart")
    public String calCart(String[] bids,HttpServletRequest request){
        log.info("结算的商品ID ：{}",bids);
        TUser user = (TUser) request.getSession().getAttribute("userInfo");
        //根据商品ID 查询 购物车中的数据
        Collection<Object> cals = cartService.findCartByCal(user,bids);
        BigDecimal all = new BigDecimal("0");
        if (cals != null && cals.size() > 0){
            //计算总价格
            for (Object o :cals){
                Cart c = (Cart) o;
                all = all.add(c.getSumprice());
            }
        }
        //根据用户查询  用的地址
        List<TAddress> addressList = addressService.findAddressByUser(user);
        request.setAttribute("cals",cals);
        request.setAttribute("addressList",addressList);
        request.setAttribute("allPrice",all);
        return "calCart" ;
    }

    @RequestMapping("/cart/showCart")
    public String showCart(HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("userInfo");
        Collection<Object> carts = cartService.findCartByUser(user);

        request.setAttribute("cart",carts);
        return "cart" ;
    }



    @RequestMapping("/cart/addCart")
    @ResponseBody
    public String addCart(Long bookId, int buyCount, HttpServletRequest request){
        try {
            log.info("当前添加商品的ID:{}",bookId);
            log.info("当前购买商品的数量:{}",buyCount);
            TUser user = (TUser) request.getSession().getAttribute("userInfo");
            cartService.addCart(bookId,buyCount,user);
            return "1" ;
        }catch (Exception e) {
            e.printStackTrace();
            return  "0" ;
        }
    }

    @RequestMapping("/cart/modifyCart")
    @ResponseBody
    public String modifyCart(Long bookId,int buyCount,HttpServletRequest request){
        try {
            log.info("当前修改商品的ID:{}",bookId);
            log.info("当前修改商品的数量:{}",buyCount);
            TUser user = (TUser) request.getSession().getAttribute("userInfo");
            cartService.modifyCart(bookId,buyCount,user);
            return "1" ;
        }catch (Exception e) {
            e.printStackTrace();
            return  "0" ;
        }
    }

    @RequestMapping("/cart/delCart")
    @ResponseBody
    public String delCart(Long bookId,HttpServletRequest request){
        try {
            log.info("当前删除商品的ID:{}",bookId);
            TUser user = (TUser) request.getSession().getAttribute("userInfo");
            cartService.delCart(bookId,user);
            return "1" ;
        }catch (Exception e) {
            e.printStackTrace();
            return  "0" ;
        }
    }

    @RequestMapping("/cart/delBatchCart")
    @ResponseBody
    public String delBatchCart(@RequestParam(value = "bookIds[]") String[] bookIds, HttpServletRequest request){
        try {
            log.info("当前删除商品的ID:{}",bookIds);
            TUser user = (TUser) request.getSession().getAttribute("userInfo");
            cartService.delBatchCart(bookIds,user);
            return "1" ;
        }catch (Exception e) {
            e.printStackTrace();
            return  "0" ;
        }
    }

    @RequestMapping("/cart/clearCart")
    @ResponseBody
    public String clearCart(HttpServletRequest request){
        try {
            TUser user = (TUser) request.getSession().getAttribute("userInfo");
            cartService.clearCartuser(user);
            return "1" ;
        }catch (Exception e) {
            e.printStackTrace();
            return  "0" ;
        }
    }
}
